package com.iatinspur.controller;


import com.iatinspur.context.UserContext;
import com.iatinspur.listener.event.MyTestEvent;
import com.iatinspur.service.ThreadLocalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;


/**
 * 通过 com.iatinspur.learn2.Learn_8_JVM描述   我想对ThreadLocal研究一下
 */
@Slf4j
@RestController
public class ThreadLocalController {

    @Autowired
    private ThreadLocalService service;

    @Autowired
    private ApplicationContext context;


    @GetMapping("/threadLocalTest")
    public String getResult2(){
        String uuid = UUID.randomUUID().toString();
        log.info("ThreadLocalController:【{}】",uuid);
        UserContext.claer();
        UserContext.set(uuid);
        service.useThreadLocal();

        MyTestEvent event =new MyTestEvent("ThreadLocalController【"+uuid+"】");
        event.setLog(log);
        context.publishEvent(event);

        Thread.currentThread().interrupt();

        return "success";
    }
}
